package com.bobo.servicepay.task;

import com.bobo.servicepay.dao.TbEventServicePayDao;
import com.bobo.servicepay.entry.TbEventServicePay;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.jms.Queue;
import java.util.List;

/**
 * @author bobo
 * @date 2021-04-06
 */
@Component
public class ProduceTask {
    @Autowired
    private TbEventServicePayDao tbEventServicePayDao;

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Scheduled(cron = "0/5 * * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void task(){
        System.out.println("定时任务");
        List<TbEventServicePay> serviceOrders = tbEventServicePayDao.selectByOrderType(1);
        for (TbEventServicePay servicePay : serviceOrders) {
            tbEventServicePayDao.updateOrderType(servicePay.getOrderType());
            System.out.println("修改数据库完成");

            // 发向消息队列
            jmsMessagingTemplate.convertAndSend(queue, JSONObject.fromObject(servicePay).toString());
        }
    }
}
